//
//  addAdressCell.m
//  NewPro
//
//  Created by 何荧红 on 2018/10/8.
//  Copyright © 2018 何荧红. All rights reserved.
//

#import "addAdressCell.h"
#import "ScanQRViewController.h"

@interface addAdressCell ()
@property (nonatomic, assign) NSInteger lesstime;

@end
@implementation addAdressCell

- (void)awakeFromNib {
    [super awakeFromNib];
    self.lesstime = 60;
    [self.detailtext addTarget:self action:@selector(textFieldDidEditing:) forControlEvents:UIControlEventEditingChanged];
}
-(void)textFieldDidEditing:(UITextField *)textField{
    if (self.detailBlock) self.detailBlock(textField.text);
}
- (IBAction)buttonClik:(UIButton *)sender {
    if (!sender.selected) {
        if ([sender.titleLabel.text isEqualToString:@"获取验证码"]) {
            
        }
        if ([sender.titleLabel.text isEqualToString:@"自动识别"]) {
            ScanQRViewController *VC = [[ScanQRViewController alloc] init];
            [[self getCurrentViewController].navigationController pushViewController:VC animated:YES];
        }
        if (self.sendBlock) self.sendBlock();
    }
    sender.selected = !sender.selected;
}
- (void)daojishi {
    __block int timeout = (int)_lesstime; //倒计时时间
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_source_t _timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0,queue);
    dispatch_source_set_timer(_timer,dispatch_walltime(NULL, 0),1.0*NSEC_PER_SEC, 0); //每秒执行
    dispatch_source_set_event_handler(_timer, ^{
        if(timeout <= 0){ //倒计时结束，关闭
            dispatch_source_cancel(_timer);
            dispatch_async(dispatch_get_main_queue(), ^{
                //设置界面的按钮显示 根据自己需求设置
                [self.sendbutton setTitle:@"获取验证码" forState:UIControlStateNormal];
                self.sendbutton.enabled = YES;
            });
        }else{
            NSString *strTime;
            if (timeout == 60) {
                strTime = [NSString stringWithFormat:@"%ld",self.lesstime];
            }else {
                int seconds = timeout % self.lesstime;
                strTime = [NSString stringWithFormat:@"%.2d", seconds];
            }
            dispatch_async(dispatch_get_main_queue(), ^{
                [self.sendbutton setTitle:[NSString stringWithFormat:@"%@s",strTime] forState:UIControlStateNormal];
                self.sendbutton.enabled = NO;
            });
            timeout--;
        }
    });
    dispatch_resume(_timer);
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
}

@end
